'''DataK EZ protocol

 This protocol is the most basic one as it uses only default strategies.
 This is the default protocol.
 
# LICENSE ....................................................................
    This file is part of DataK.

    DataK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DataK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DataK.  If not, see <http://www.gnu.org/licenses/>.
# ............................................................................

'''

import _base
import item
import strategy

class EZ( _base._Base ):
    '''
    EZ is the default DataK protocol.
    It uses default strategies:
        linux symbolic links
        no access/revision control
        no mirroring
        no mailing
        log if requested (Item and Links attributes only.)
    
    Options:
        'logFile'
            Bbsolute path of the log file. Needs to reconnect if changed.
    '''
    def __init__( self, config_string='' ):
        '''
        Create the protocol and configure it as described by config_string.
        '''
        _base._Base.__init__( self, configString )
        # access/revision strategy is unic for all items
        # and needs self.user wich is not yet set, so we
        # just create the slot and will instanciate it
        # only when needed (in setup_item)
        self.rc = None
        
        # attribute strategy depends on the item class
        logFile = self.config.get('logFile')
        self.attrStrategies = {
            item.Item:  strategy.AttributeStrategy(logFile),
            item.Link:  strategy.LinkAttributeStrategy(logFile),
            item.Group: strategy.GroupAttributeStrategy(),
        }
        
        # select strategy depends on the item class
        ss = strategy.SelectStrategy()
        self.selectStrategies = {
            item.Item:  ss,
            item.Link:  ss,
            item.Group: strategy.GroupSelectStrategy(),
        }
   
    def setup_item( self, item ):
        '''
        Set all strategies on item to follow the protocol rules.
        
        Strategies used here are from the strategy module:
            RCStrategy
            AttributeStrategy/LinkAttributeStrategy/GroupAttributeStrategy
            SelectStrategy/SelectStrategy/GroupSelectStrategy
            SortStrategy
            GroupStrategy
        '''
        if self.rc is None:
            # create the revision control strategy if not done yet:
            self.rc = strategy.RCStrategy( user=self.user, needsLock=False )
            
        item._rc = self.rc
        item._attrs = self.attrStrategies[item.__class__]
        item._selecter = self.selectStrategies[item.__class__]
        item._sorter = strategy.SortStrategy()
        item._grouper = strategy.GroupStrategy()


